package com.example.administrator.startproject_108zct;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.startproject_108zct.bean.WeatherInfo;
import com.example.administrator.startproject_108zct.iface.WeatherListener;
import com.example.administrator.startproject_108zct.model.Weather51Model;
import com.example.administrator.startproject_108zct.model.WeatherModel;

public class Startproject extends AppCompatActivity implements WeatherListener,View.OnClickListener {

    private EditText etcity;
    private Button btnfind1, btnfind2;
    private TextView tvcityname;
    private TextView cityNO;
    private TextView temp;
    private TextView wd;
    private TextView ws;
    private TextView sd;
    private ProgressDialog loadingWeather;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startproject);
        init();
    }

    private void init() {
        etcity = (EditText) findViewById(R.id.editText);
        btnfind1 = (Button) findViewById(R.id.button);
        btnfind2 = (Button) findViewById(R.id.button2);
        tvcityname = (TextView) findViewById(R.id.textView2);
        cityNO = (TextView) findViewById(R.id.tv_city_no);
        temp = (TextView) findViewById(R.id.tv_temp);
        wd = (TextView) findViewById(R.id.tv_WD);
        ws = (TextView) findViewById(R.id.tv_WS);
        sd = (TextView) findViewById(R.id.tv_SD);
        loadingWeather = new ProgressDialog(this);
        loadingWeather.setTitle("查询中...");
        btnfind1.setOnClickListener(this) ;
        btnfind2.setOnClickListener(this) ;
    }

    public void onResponse(WeatherInfo weather) {
        //取消数据加载进度条
        loadingWeather.dismiss();
        //显示获得的天气数据
        if (weather != null) {
            tvcityname.setText(weather.getCity());
            cityNO.setText(weather.getCityid());
            temp.setText(weather.getTemp());
            wd.setText(weather.getWD());
            ws.setText(weather.getWS());
            sd.setText(weather.getSD());
        } else {
            tvcityname.setText("未知");
        }
    }
    @Override
    public void onFail(String msg) {
        tvcityname.setText(msg);
    }
    @Override
    public void onClick(View v) {
        loadingWeather.show();
        switch(v.getId())
        {
            case R.id.button://中国天气
                //普通创建对象方式
                WeatherModel weatherModel = new WeatherModel();
                weatherModel.getWeather(etcity.getText().toString(), this);
                //工厂模式
                break;
            case R.id.button2://51天气
                Weather51Model weather51Model = new Weather51Model();
                weather51Model.getWeather(etcity.getText().toString(), this);
                break;
            default:
                break;
        }

    }
}